#ifndef CONFIG_H
#define CONFIG_H

#include <QSettings>
#include <QString>
#include <QStringList>
#include <QIcon>
#include <QDir>
#include <QApplication>
#include <QStringList>
#include <QDateTime>
#include <QDesktopServices>
#include <QDebug>
#include <QUrl>
#include <QImageReader>
#include <QImageWriter>
#include <QProcess>
#include <QTextDocumentWriter>
#include <QElapsedTimer>


class AppInfo
{
private:
    QString m_name;
    QString m_commandLine;
    QString m_iconPath;

    inline const QString iconFullPath () const
    {
        QString result (m_iconPath);
        const QFileInfo &fi (result);
        if (!fi.exists ()) {
            result = QString ("/usr/share/icons/%1").arg (m_iconPath);
            if (fi.suffix ().isEmpty ()) {
                result += ".png";
            }
        }
        return result;
    }

public:
    AppInfo () {}
    const QString &name () const { return m_name; }
    const QString &commandLine () const { return m_commandLine; }

    void setName (const QString &name) { m_name = name; }
    void setCommandLine (const QString &cl) { m_commandLine = cl; }
    void setIconPath (const QString &iconPath) { m_iconPath = iconPath; }

    // il est compliqué ce linux avec ses astuces peu documentées
    // soit l'icône est dans /usr/share/icons avec l'extension png
    // soit il faut l'extraire d'un thème
    const QIcon icon () const { return QIcon::fromTheme (m_iconPath, QIcon (iconFullPath ())); }
};


class Config : public QSettings
{
    Q_OBJECT

private:

public:
    explicit Config (QObject *parent = 0) : QSettings (parent) {}
    ~Config () { sync (); }

    static const QString appVersion () { return "14.04.29"; }
    static const QString dicVersion () { return "12.01.12"; }
    static const QString docVersion () { return "12.01.28"; }
    static const QString appName () { return "solidROC"; }
    static const QString scanAppName () { return "scaneton"; }
    static const QString appWorkPath ()
    {
        return QDir::tempPath ()
                + '/'
                + appName ()
                + '/';
    }
    static const QString appHomePath ()
    {
        return QDir::homePath ()
                + "/."
                + appName ()
                + '/';
    }
    static const QUrl docFileUrl ();

    static const QString docFilename () { return "solidDOC_" + sessionLanguage () + ".html"; }

    static const QString sessionLanguage () { return QLocale::system ().name ().left (2); }
    static const QString appIconPath () { return ":/images/solidROC.svg"; }
    static const QIcon appIcon () { return QIcon (appIconPath ()); }
    static const QStringList docPossiblePaths ();
    static const QString sessionsConfPath () { return appHomePath () + "sessions.conf"; }
    static const QUrl noDocAvailable () { return QUrl::fromLocalFile ("qrc:/noDoc.html"); }
    static const QString appExportPath () { return appHomePath () + "export/"; }
    static bool ODFSupport () { return QTextDocumentWriter::supportedDocumentFormats ().contains ("ODF"); }
    static const QString personalDicPath () { return appHomePath () + "perso.dic"; }
    static const QString translateDicPath () { return appHomePath () + "translate.dic"; }

    const QString scannedImageCompleteFileName ();
    static const QString exportedTextCompleteFileName ();

    static const QString dateFormat () { return "yyyy-MM-dd-hh-mm-ss-zzz"; }
    static QString tempBaseName () { return QDateTime::currentDateTime ().toString (dateFormat ()) ; }
    static QString tempFullName () { return appWorkPath () + tempBaseName (); }
    static QString tempTxtBaseName () { return tempBaseName () + ".txt"; }
    static QString tempODTBaseName () { return tempBaseName () + ".odt"; }
    static QString tempTifBaseName () { return tempBaseName () + ".tif"; }
    static const QString fileNameForTesseract ()
    {
        QString result (QDir::toNativeSeparators (appWorkPath () + "deSolidROC-" + tempTifBaseName ()));
        QFileInfo fi (result);
        return fi.exists () ? fileNameForTesseract () : result;
    }

    static const QString baseNameFromScanner () { return QDir::toNativeSeparators ("scan-"  + tempTifBaseName ()); }

    static const QString defaultPathForScannedImages ()
    {
//        return QDir::toNativeSeparators (QDesktopServices::storageLocation (QDesktopServices::PicturesLocation)
//                                         + '/');
        return "/tmp";
    }
    static const QIcon symbol (const QString &fileName);
    static QString shell (const QString &cmdLine);
    static QString bash (const QString &cmdLine);
    static const QList<AppInfo> appsFromType (const QString &type, const QString &papajaacApp = QString ());
    static const QList<AppInfo> wordProcessors();
    static const QStringList wordProcessorNames ();
    static const QStringList loadFilters ();
    static const QString saveFilters ();
    static const QString documentLinuxType (const QString &path);

    QString currentWordProcessorCommandLine ();

    bool optionRestoreWorkSession () const;
    bool optionSimpleAnimation () const;
    int optionRecentFileMax () const;
    bool optionRemoveTemp () const;
    bool optionUseXSane () const;
    bool optionAllSaneOptions() const;
    bool optionAutoSaveScannedFiles () const;
    QString optionPathForScannedFiles () const;
    bool optionUsePdfToText () const;
    int optionResolutionForPdf () const;
    int optionWordProcessorIndex () const;
    bool optionDontAskForWP () const;
    bool optionAskForWP() const;
    bool optionCRBetweenROI () const;
    int optionsToolboxPage () const;
    bool optionRememberGeometry () const;
    bool optionInvertOrphanPixels () const;
    int optionMinPixNeighbours() const;
    bool optionExportTextToODF() const;
    bool optionRemoveCaesura() const;
    bool optionReplaceCRBySpace() const;
    bool optionInvertColors () const;


    QFont					optionDefaultFont ()	const;
    QColor					optionUnderlineColor ()	const;
    QByteArray				optionSaveFormat ()		const;

    QSize windowSize() const;
    QPoint windowPos() const;
    bool windowIsMaximized()  const;
    bool windowIsMinimized() const;
    void setLastGeometry(const QPoint& p,
                         const QSize& s,
                         const bool maximized,
                         const bool minimized);
    QString lastOpenDocPath () const;
    void setLastOpenDocPath (const QString& ba);
    QByteArray lastSplitterState ();
    void setLastSplitterState (QByteArray const &ba);

public slots:
    void setOptionRestoreWorkSession (const bool b);
    void setOptionSimpleAnimation (const bool b);
    void setOptionRecentFileMax (const int val);
    void setOptionRemoveTemp (const bool b);
    void setOptionUseXSane (const bool b);
    void setOptionAllSaneOptions(const bool b);
    void setOptionAutoSaveScannedFiles (const bool b);
    void setOptionPathForScannedFiles (const QString & s);
    void setOptionUsePdfToText (const bool b);
    void setOptionResolutionForPdf (const int d);
    void setOptionWordProcessorIndex (const int index);
    void setOptionDontAskForWP (const bool b);
    void setOptionCRBetweenROI (const bool b);
    void setOptionsToolboxPage (const int p);
    void setOptionRememberGeometry (const bool b);
    void setOptionInvertOrphanPixels (const bool b);
    void setOptionMinPixNeighbours (const int val);
    void setOptionExportTextToODF(const bool b);
    void setOptionRemoveCaesura(const bool b);
    void setOptionReplaceCRBySpace(const bool b);
    void setOptionDefaultFont (const QFont & font);
    void setOptionUnderlineColor (const QColor & color);
    void setOptionSaveFormat (const QString & format);
    void setOptionInvertColors (bool const b);
};

#endif // CONFIG_H
